package edu.fzu.fundmgr.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.fzu.fundmgr.domain.Client;
import edu.fzu.fundmgr.domain.Fund;
import edu.fzu.fundmgr.service.FundService;
import edu.fzu.fundmgr.service.impl.FundServiceImpl;

public class FundMgrServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void loadAll(HttpServletRequest request, HttpServletResponse response) {
		FundService fundService = new FundServiceImpl();
		List<Fund> fundList = fundService.loadAll();

		request.setAttribute("funds", fundList);

		try {
			request.getRequestDispatcher("show_funds.jsp").forward(request,
					response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		String act = request.getParameter("act");

		if (act.equals("add")) {

			Fund fund = new Fund();

			// fund.setFundNO(Integer.parseInt(request.getParameter("fund_no")));
			fund.setFundName(request.getParameter("fund_name"));
			fund.setPrice(Double.parseDouble(request.getParameter("price")));
			System.out.println(request.getParameter("description"));
			if(request.getParameter("description")==null)
				fund.setDescription("");
			else
				fund.setDescription(request.getParameter("description"));
			fund.setStatus(request.getParameter("status"));
			fund.setCreatedDate(new Date());

			FundService fundService = new FundServiceImpl();
			fundService.addFund(fund);

			// response.sendRedirect("fundMgr?act=load_all");
			loadAll(request, response);
		} else if (act.equals("load_all")) {
			loadAll(request, response);
		} else if (act.equals("del")) {

			int fundNO = Integer.parseInt(request.getParameter("fund_no"));

			FundService fundService = new FundServiceImpl();
			fundService.removeFund(fundNO);

			loadAll(request, response);
		} else if (act.equals("get_fund")) {

			int fundNO = Integer.parseInt(request.getParameter("fund_no"));

			FundService logService = new FundServiceImpl();
			request.setAttribute("fund", logService.getFund(fundNO));

			request.getRequestDispatcher("update_costlog.jsp").forward(request,
					response);
		} else if (act.equals("update")) {

			Fund fund = new Fund();

			fund.setFundName(request.getParameter("fund_name"));
			fund.setPrice(Double.parseDouble(request.getParameter("price")));
			fund.setDescription(request.getParameter("description"));
			fund.setStatus(request.getParameter("status"));
			fund.setCreatedDate(new Date());

			FundService fundService = new FundServiceImpl();
			fundService.updateFund(fund);

			loadAll(request, response);
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
